﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DevPhone.Controls;
using DevPhoneToolkitSample.Data;
using Microsoft.Phone.Controls;

namespace DevPhoneToolkitSample.Samples
{
    public partial class CalendarControlDateMarkSample : PhoneApplicationPage
    {
        private DateTime SelectedDate;
        private static IEnumerable<ColorItem> CreateListColors()
        {
            return new List<ColorItem>()
                       {
                           new ColorItem {Text = "Default", Color = Colors.Transparent},
                           new ColorItem {Text = "orange", Color = Colors.Orange},
                           new ColorItem {Text = "red", Color = Colors.Red},
                           new ColorItem {Text = "blue", Color = Colors.Blue},
                           new ColorItem {Text = "magenta", Color = Colors.Magenta},
                           new ColorItem {Text = "purple", Color = Colors.Purple},
                           new ColorItem {Text = "green", Color = Colors.Green},
                           new ColorItem {Text = "cyan", Color = Colors.Cyan},
                           new ColorItem {Text = "brown", Color = Colors.Brown},
                           new ColorItem {Text = "yellow", Color = Colors.Yellow},
                       };
        }

        public CalendarControlDateMarkSample()
        {
            InitializeComponent();
            CboColor.ItemsSource = CreateListColors();
            CboColor.SelectedIndex = 0;
            foreach (var item in CboMarkWidth.Items.Cast<DropDownItem>().Where(item => item.Content.ToString() == Calend.MarkWidth.ToString()))
            {
                CboMarkWidth.SelectedItem = item.Content.ToString();
                break;
            }
        }

        private void CalendarControlChangeSelectDate(object sender,
                                                     DevPhone.Controls.CalendarChangeSelectDateEventArgs e)
        {
            expopt.IsExpanded = false;
            OptDisabled.IsEnabled = true;
            OptMark.IsEnabled = true;
            BntRemove.IsEnabled = false;
            SelectedDate = e.Date;
            DateSelect.Text = e.Date.ToShortDateString();
            var mark = Calend.ColorDays().FirstOrDefault(d => d.Date == e.Date);
            if (mark == null)
            {
                var dmark = Calend.DisabledDates().Any(d => d == e.Date);
                if (dmark)
                {
                    OptDisabled.IsChecked = true;
                    BntRemove.IsEnabled = true;
                    CboDateMarkWidth.Visibility = Visibility.Collapsed;
                    CboColor.Visibility = Visibility.Collapsed;
                    OptDisabled.IsEnabled = false;
                    OptMark.IsEnabled = false;
                }
                else
                {
                    CboColor.SelectedIndex = 0;
                    OptMark.IsChecked = true;
                }
            }
            else
            {
                OptMark.IsChecked = true;
                OptDisabled.IsEnabled = false;
                BntRemove.IsEnabled = true;
                OptMark.IsEnabled = false;
                CboColor.Visibility = Visibility.Visible;
                var found = false;
                foreach (var item in CboColor.ItemsSource)
                {
                    if (((ColorItem)item).Color.Equals(((SolidColorBrush)mark.ColorBrushMark).Color))
                    {
                        CboColor.SelectedItem = item;
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    CboColor.SelectedIndex = 0;
                }
                CboDateMarkWidth.SelectedItem = mark.Width.ToString();
                CboDateMarkWidth.Visibility = Visibility.Visible;
            }
            AreaCalend.Visibility = Visibility.Collapsed;
            PopupSelection.Visibility = Visibility.Visible;
        }

        private void CboMarkPositionSelectionChange(object sender, SelectionChangedEventArgs e)
        {
            Calend.MarkPosition =
                (CalendarPositionMark) Enum.Parse(typeof (CalendarPositionMark), e.AddedItems[0].ToString(), true);
        }

        private void CboMarkWidthSelectionChange(object sender, SelectionChangedEventArgs e)
        {
            if (CboMarkWidth.SelectedIndex == 0)
            {
                Calend.MarkWidth = CalendarMarkWidth.Default;
            }
            else
            {
                Calend.MarkWidth =
                    (CalendarMarkWidth)Enum.Parse(typeof(CalendarMarkWidth), e.AddedItems[0].ToString(), true);
            }
        }

        private void OptMarkClick(object sender, RoutedEventArgs e)
        {
            CboColor.SelectedIndex = 0;
            CboDateMarkWidth.Visibility = Visibility.Visible;
            CboColor.Visibility = Visibility.Visible;
        }

        private void OptDisabledClick(object sender, RoutedEventArgs e)
        {
            CboDateMarkWidth.Visibility = Visibility.Collapsed;
            CboColor.Visibility = Visibility.Collapsed;
        }

        private void BntCancelClick(object sender, RoutedEventArgs e)
        {
            PopupSelection.Visibility = Visibility.Collapsed;
            AreaCalend.Visibility = Visibility.Visible;
        }

        private void BntOkClick(object sender, RoutedEventArgs e)
        {
            if (OptDisabled.IsChecked.Value)
            {
                Calend.AddDisabledDate(SelectedDate);
            }
            else
            {
                if (CboColor.SelectedIndex == 0)
                {
                    Calend.AddDateColor(SelectedDate,Calend.Foreground,(CalendarMarkWidth)Enum.Parse(typeof(CalendarMarkWidth),CboDateMarkWidth.SelectedItem.ToString(),true));
                }
                else
                {
                    var selcolor = new SolidColorBrush(((ColorItem)CboColor.SelectedItem).Color);
                    Calend.AddDateColor(SelectedDate, selcolor, (CalendarMarkWidth)Enum.Parse(typeof(CalendarMarkWidth), CboDateMarkWidth.SelectedItem.ToString(), true));
                }
            }
            PopupSelection.Visibility = Visibility.Collapsed;
            AreaCalend.Visibility = Visibility.Visible;

        }

        private void BntRemoveClick(object sender, RoutedEventArgs e)
        {
            if (OptDisabled.IsChecked.Value)
            {
                Calend.RemoveDisabledDate(SelectedDate);
            }
            else
            {
                Calend.RemoveDateColor(SelectedDate);
            }
            PopupSelection.Visibility = Visibility.Collapsed;
            AreaCalend.Visibility = Visibility.Visible;

        }

        private void BntResetDisabledDatesClick(object sender, RoutedEventArgs e)
        {
            Calend.ClearDisabledDate();
        }

        private void BntResetMarkDatesClick(object sender, RoutedEventArgs e)
        {
            Calend.ClearDateColor();
        }

    }
}